<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Vod\V20180717\Models\DeleteMediaRequest;
use TencentCloud\Vod\V20180717\Models\SearchMediaRequest;
use TencentCloud\Vod\V20180717\VodClient;

class delVod extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:delVod {--months=}';

    /**
     * The console command description.
     * 0 1 * * *
     * @var string
     */
    protected $description = '删除腾讯云点播视频';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        //保留几个月的数据
        $months = $this->option("months") ?? 6;
        $limitDate = date("Y-m-d", strtotime("- {$months} months"));
        $cred = new Credential("AKIDJRIuEH3YooWx82h5C2bssc5HBpDJ8hzV", "HH0SdW9WeaIkwsYzBD1bw3wb1C8rLZYI");
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        $httpProfile = new HttpProfile();
        $httpProfile->setEndpoint("vod.tencentcloudapi.com");

        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        $clientProfile = new ClientProfile();
        $clientProfile->setHttpProfile($httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        $client = new VodClient($cred, "", $clientProfile);

        // 实例化一个请求对象,每个接口都会对应一个request对象
        $req = new SearchMediaRequest();
        $limit = 5000;
        $params = array(
            "Categories" => ["Video"],
            "Offset" => 0,
            "Limit" => $limit,
            "Filters" => ['basicInfo'],
            "Sort" => ["Field" => "CreateTime", "Order" => "Asc"],
            //"CreateTime" => ["After" => "2023-09-11T00:00:00+08:00", "Before" => "2023-09-12T00:00:00+08:00"]
        );
        $isStop = false;
        while (true){
            if($isStop){
                break;
            }
            $req->fromJsonString(json_encode($params));
            // 返回的resp是一个SearchMediaResponse的实例，与请求对象对应
            $resp = json_decode(($client->SearchMedia($req))->toJsonString(), true);
            $total = count($resp['MediaInfoSet']);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            $delReq = new DeleteMediaRequest();
            foreach ($resp['MediaInfoSet'] as $i => $item){
                if(date("Y-m-d", strtotime($item['BasicInfo']['CreateTime'])) >= date('Y-m-d', strtotime($limitDate))){
                    echo "当前获取时间：".date("Y-m-d", strtotime($item['BasicInfo']['CreateTime']))."， 超过时间阈值：{$limitDate}，暂停删除计划\n";
                    $isStop = true;
                    break;
                }
                echo "共{$total}, 正删除第 ".($i+1)." 条; 编码: {$item['FileId']}, 文件名: {$item['BasicInfo']['Name']}, 创建时间: {$item['BasicInfo']['CreateTime']}, 阈值：{$limitDate}\n";
                $delReq->fromJsonString(json_encode(["FileId" => $item['FileId']]));
                $client->DeleteMedia($delReq);
            }
        }
    }
}
